<?php
	// prendre les forum_topic = fid
	$sql = "SELECT * FROM forum_topic as ft WHERE ft.forum_id=".$_REQUEST['fid'].'  ORDER BY topic_type ASC,topic_time DESC LIMIT '.($_GET['num']-1).','.($_GET['num']*25);
	$q = $pdo->query($sql);
	

	$_page .= '<div class="cadre_transparent"><h1>'.$titre['forum_name'].'</h1></div>';
	
	$topic_type = '';$flag=false;
	
	foreach($q as $topicLine) 
	{
		$sql2 = "SELECT * FROM forum_post WHERE topic_id=".$topicLine['topic_id']." ORDER BY post_time DESC LIMIT 1";
	$sqll = $pdo->prepare($sql2);
	$sqll->execute();
	
	$q2 = $sqll->fetch();
	$sqll->closeCursor();
	
		if($topic_type != $topicLine['topic_type']){
			if($topicLine['topic_type']==1){
				
				
				$totalPostCat = $pdo->query("SELECT post_id FROM forum_post AS fp 
										 LEFT JOIN forum_topic AS ft ON fp.topic_id=ft.topic_id
										 LEFT JOIN forum_forum AS ff ON ff.forum_id=ft.forum_id
										 JOIN forum_categorie AS fc ON fc.cat_id=ff.forum_cat_id 
										 WHERE ff.forum_id=".intval($_REQUEST['fid']). " AND topic_type=1  GROUP BY ft.topic_id HAVING COUNT(fp.post_id) > 1 ");
							$ReqtotalTopicVu = $pdo->query("SELECT SUM(topic_vu) as nb_topic_vu FROM forum_topic AS ft
										 LEFT JOIN forum_forum AS ff ON ff.forum_id=ft.forum_id
										 WHERE ff.forum_id=".intval($_REQUEST['fid'])." AND topic_type=1 GROUP BY ff.forum_id");
							$totalTopicVu = $ReqtotalTopicVu->fetch(PDO::FETCH_OBJ);
						
				if($flag)
				{
					$_page .= '';
				}
				$flag = true;
				$_page .= '';
				$_page .= '<div class="cadre_visible contenu_forum"><div class="titre_forum">Annonces importantes
				<span class="texte_droite1">' . $totalPostCat->rowCount() .' Réponses ' . $totalTopicVu->nb_topic_vu . ' Vues</span></div>';
				
							}else{
								
				if($flag)
				{
					$_page .= '</div>';
				}
							$totalPostCat = $pdo->query("SELECT post_id FROM forum_post AS fp 
										 LEFT JOIN forum_topic AS ft ON fp.topic_id=ft.topic_id
										 LEFT JOIN forum_forum AS ff ON ff.forum_id=ft.forum_id
										 JOIN forum_categorie AS fc ON fc.cat_id=ff.forum_cat_id 
										 WHERE ff.forum_id=".intval($_REQUEST['fid']). " AND topic_type=2  GROUP BY ft.topic_id HAVING COUNT(fp.post_id) > 1 ");
							$ReqtotalTopicVu = $pdo->query("SELECT SUM(topic_vu) as nb_topic_vu FROM forum_topic AS ft
										 LEFT JOIN forum_forum AS ff ON ff.forum_id=ft.forum_id
										 WHERE ff.forum_id=".intval($_REQUEST['fid'])." AND topic_type=2 GROUP BY ff.forum_id");
							$totalTopicVu = $ReqtotalTopicVu->fetch(PDO::FETCH_OBJ);
							
			$_page .= '<div class="cadre_visible contenu_forum"><div class="titre_forum">Topic
			<span class="texte_droite1">' . $totalPostCat->rowCount() .' Réponses & ' . $totalTopicVu->nb_topic_vu . ' Vues</span></div>';

			}
			$topic_type = $topicLine['topic_type'];
			
		}
		
		$pseudo = sex2color(id2sex($topicLine['topic_forum_membre_id']),$topicLine['topic_forum_membre_id']);
		$query = 'SELECT count(*) as value FROM forum_post WHERE topic_id='.$topicLine['topic_id'];
		$fvquery = 'SELECT * FROM forum_forum_view JOIN forum_post ON fv_post_id=post_id WHERE fv_membre_id=? AND fv_topic_id=? AND fv_post_id=?';
		$fv = $pdo->prepare($fvquery);
		
		if(isset($_SESSION['id']))
			$fv->execute(array($_SESSION['id'],$topicLine['topic_id'],$q2['post_id']));
		else
			$fv->execute(array(-1,$topicLine['topic_id'],$q2['post_id']));
		
		$cnt = GetValueFromQuery($query);
		
			if(!$fv->rowCount())
				$syslu = '<img src="'.DIR_DESIGN.'/'.getDesignName().'/images/forum/non_lu.gif" title="Nouvelles réponses" height="20"/>';
			else
				$syslu = '<img src="'.DIR_DESIGN.'/'.getDesignName().'/images/forum/lu.gif" title="Pas de nouvelles réponses" height="20"/>';
				
			if($topicLine['topic_resolu'] == '1')
				$resolu = '<img src="'.DIR_DESIGN.'/'.getDesignName().'/images/forum/resolu.png" title="Le sujet du topic à été résolu" height="20"/>';			
			else
				$resolu = '<img src="'.DIR_DESIGN.'/'.getDesignName().'/images/forum/pas_resolu.gif" title="Le sujet du topic n\'est pas encore résolu" height="20"/>';	
				
				$locked = '';
				
					if($topicLine['topic_locked'] == '1')
						$locked = '<img src="'.DIR_DESIGN.'/'.getDesignName().'/images/forum/lock.png" title="Le sujet est vérouillé" height="20"/>';	



$_page .= '<div class="hover_du_categorie_forum hover_du_categorie_noforum parente">
           <div class="topic_systeme_lu_non_lu">'. $locked . $syslu .'</div>
           <div class="topic_emoticone"><img src="'.DIR_DESIGN.'/'.getDesignName().'/images/smileys/topic/huh.png" title="Le sujet du topic n\'est pas encore résolu" height="20"/></div>
		   <div class="topic_sujet">'. $resolu. '<a href="http://'.$_SERVER['SERVER_NAME'].(getLienModule($_GET["m"],"voirmessage") .'?fid='.$_GET['fid'].'&tid='.$topicLine['topic_id']).'&num=1">'.($topicLine['topic_titre']).'</a>
		   <br />Auteur : '.$pseudo.'</div>
		   <div class="topic_rep_vue">'.($cnt-1).' réponses<br />'.($topicLine['topic_vu']).' Vues</div>
		   <div class="topic_auteur">'.GetSuperMiniAvatarById( $q2 ["forum_membre_id"] ) .'</div>
		   <div class="topic_dernier_message">'.LastPostedBy($q2['topic_id'],$q2['forum_membre_id']).'</div>
		   </div>';



				  
	}
			$_page .= '</div>';



	echo $_page;
?>